<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class sfcarController extends CommonController
{
    //
    /**
     *
     */
     protected $sfCar;
//    public function __construct()
//    {
////        $this->Cars = array();
////        parent::__construct();
//
//    }

    public function index()
    {
       $data =  DB::table("orders")->get();
//        dd($data[0]->borrow_menid);
       $sfCarsID = array();
       foreach($data as $k=>$v){
           $city_id = DB::table("car")->where("id",$data[$k]->car_id)->value("zc_store_menid");

           $startCity = DB::table("store")->where("menid",$city_id)->pluck("zc_city_ctid");
           $endCity = DB::table("store")->where("menid",$data[$k]->return_menid)->pluck("zc_city_ctid");
           if($startCity != $endCity){//还车和取车城市不同，即为顺风车
                $sfCarsID["car_id"][] = $v->car_id;
                $sfCarsID["ord"][] = $data[$k];
           }
       }
//      dd($sfCarsID);
        $sfCars =array();//返回的所有信息
        $Citys = array();//定义有顺风车的城市
        foreach($sfCarsID["car_id"] as $k=>$v){
            $sfCars[$k]["ord"] = $sfCarsID["ord"][$k];
            $borrow_c = DB::table("store")->join("city","zc_city_ctid","=","ctid")->where("menid",$data[$k]->borrow_menid)->get();
            $sfCars[$k]["ord"]->borrow_cname = $borrow_c[0]->cityname;
            $sfCars[$k]["ord"]->borrow_ctid = $borrow_c[0]->ctid;
            $return_c = DB::table("store")->join("city","zc_city_ctid","=","ctid")->where("menid",$data[$k]->return_menid)->get();
//            dd($return_c);
            $sfCars[$k]["ord"]->return_cname=$return_c[0]->cityname;
            $sfCars[$k]["ord"]->return_ctid=$return_c[0]->ctid;
            $sfCars[$k]["info"] = DB::table("car")->join("carinfo","car_id","=","id")->find($v);
            //顺风车的出发地和还车地
            $Citys[$k]["startcname"] = $return_c[0]->cityname;
            $Citys[$k]["startctid"] = $return_c[0]->ctid;
            $Citys[$k]["endcname"] = $borrow_c[0]->cityname;
            $Citys[$k]["endctid"] = $borrow_c[0]->ctid;
            $this->sfCar[]= $Citys[$k];
        }
//        $this->sf_Cars
//        dd($this->sfCar);
//        var_dump($sfCars);
//        dd($Citys);


        //筛选*****************************************************************************
       if($inp = Input::all()){
//           dd($inp);
           $sel_reslute = array();//查询结果
          if($inp["startCity"]&&!$inp["endCity"]){//只输入取车地点
//              var_dump($sfCars);
                foreach ($sfCars as $k=>$v){
                    if($v["ord"]->return_ctid ==$inp["startCity"]){
                        $sel_reslute[]=$v;
                    }
                }
              $data = [
                  "status"=>1,
                  "msg"=>$sel_reslute,
              ];
          }elseif ($inp["startCity"]&&$inp["endCity"]){//取车地点和还车地点都输入
              foreach ($sfCars as $k=>$v){
                  if($v["ord"]->return_ctid == $inp["startCity"]&&$v["ord"]->borrow_ctid == $inp["endCity"]){
                      $sel_reslute[]=$v;
                  }
              }
              $data = [
                  "status"=>1,
                  "msg"=>$sel_reslute,
              ];
          }else{//都不输入
              $data = [
                  "status"=>0,
              ];
          }
//           dd($data);
           return $data;
       }
       return view("home.shunfeng",compact("sfCars","Citys"));
    }
    //筛选
    public function selects(){
//        $inp = Input::all();
//        dd($this->sfCar);
//            dd($inp);
    }
    //book预定
    public function book($sfid=null){
//        echo session("cid");
        session(["oid"=>$sfid]);
        //取车城市
        $startCity = DB::table("city")->where("ctid",session("sfstart"))->value("cityname");
        $endCity  = DB::table("city")->where("ctid",session("sfend"))->value("cityname");
        //还车城市的所有门店
        $end_men = DB::table("store")->where("zc_city_ctid",session("sfend"))->get();
//        dd($end_men);
        //汽车目前所在门店
        $start_men = DB::table("orders")->join("store","menid","=","return_menid")->where("car_id",session("cid"))->get();
//        dd($start_men);
        //汽车信息
        $car_info = DB::table("car")->join("carinfo","id","=","car_id")->join("carmodel","zc_carModel_mid","=","mid")->where("id",session("cid"))->get();
//        dd($car_info);
        return view("home.qdsf",compact("startCity","endCity","end_men","start_men","car_info"));
    }
    //存
    public function addSESS(){
        $inp = Input::all();
        session(["cid"=>$inp['id']]);
        session(["sfstart"=>$inp['sfstart']]);
        session(["sfend"=>$inp['sfend']]);
    }
}
